package cn.sdjzu.exercise;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TestLineSaveActivity extends BasedActivity {

	ExerciseApp mApp;
	LinearLayout testLineLayout = null;
	View lineView = null;
	float[] cus;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(view.getChartView(mApp));
		setContentView(R.layout.testlinesave);		
		mApp = (ExerciseApp) TestLineSaveActivity.this.getApplication();
		testLineLayout = (LinearLayout) findViewById(R.id.customize_line);
		Intent intent =new Intent ();
		intent=getIntent();
		cus=new float[8];
		cus=intent.getFloatArrayExtra("custome");
		MyView view = new MyView(TestLineSaveActivity.this);
		lineView = view.getChartView(mApp);
		//加边框来测试曲线是否占满整个图片
		//lineView.setBackgroundResource(R.drawable.span);
		testLineLayout.addView(lineView);
	}

	public void save(View view) {
		Bitmap bitmap = convertViewToBitmap(lineView);
		bitmap=newBitmap(bitmap);
		if (bitmap == null) {
			Toast.makeText(this, "bitmap null", Toast.LENGTH_LONG).show();
		} else {
			String filePath = TestLineSaveActivity.this.getCacheDir().getAbsolutePath();
			
			Toast.makeText(TestLineSaveActivity.this, "filePath: " + filePath, Toast.LENGTH_LONG).show();
			File file = new File(filePath, "myview.png");
			if (file.exists()) {
				file.delete();
			}
			try {
				FileOutputStream out = new FileOutputStream(file);
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {				
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	private Bitmap convertViewToBitmap(View view) {

		view.buildDrawingCache();
		view.setDrawingCacheEnabled(true);
		Bitmap cacheBitmap = view.getDrawingCache();

		if (cacheBitmap == null) {
			return null;
		}

		Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
				Config.ARGB_8888);
		Canvas c = new Canvas(bitmap);
		// c.translate(-view.getScrollX(), -view.getScrollY());
		view.draw(c);
		return bitmap;
	}
	
	public void back(View view) {
		Intent intent = new Intent(this, ExerciseActivity.class);
		ExerciseActivity.currentExerciseMode = MODE_CUSTOMIZE;
		intent.putExtra("custome", cus);
		startActivity(intent);
		this.finish();
		overridePendingTransition(android.R.anim.fade_in,
				android.R.anim.fade_out);
	}
	
	public Bitmap newBitmap(Bitmap bitmap){
		Bitmap newbitmap;
		int w=bitmap.getWidth();
		int h=bitmap.getHeight();
		int neww=900;
		int newh=300;
		// 计算缩放比例
		float scaleWidth = ((float) neww) / w ;
		float scaleHeight = ((float) newh) / h;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片	
		newbitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return newbitmap;
	}
}
